﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ChinaDynastyTours.Controls;
using ChinaDynastyTours.Helpers;
using DevExpress.XtraGrid.Views.Grid;

namespace ChinaDynastyTours.Views
{
    public partial class CommissionFinanceVerifyForm : DevExpress.XtraEditors.XtraForm
    {
        private bool m_checkStatus = false;
        private DataTable _dtCommissionData;

        public CommissionFinanceVerifyForm()
        {
            InitializeComponent();
        }

        private DataTable AddGridSelectedColumn(DataTable dtSource)
        {
            dtSource.Columns.Add("Selected", typeof(bool));
            return dtSource;
        }

        private void CommissionFinanceVerifyForm_Load(object sender, EventArgs e)
        {
            #region 初始化查询条件，并首次查询

            //默认为上月1日起，下下月1日止
            dateStart.EditValue = DateTime.Parse(string.Format("{0}-{1}-01", DateTime.Now.Month > 1 ? DateTime.Now.Year : DateTime.Now.Year - 1, DateTime.Now.Month > 1 ? DateTime.Now.Month - 1 : 12));
            dateEnd.EditValue = DateTime.Parse(string.Format("{0}-{1}-01", DateTime.Now.Year + DateTime.Now.Month / 11, (DateTime.Now.Month + 1) % 12 + 1));

            //初始化部门列表
           // departmentLookupEdit.DataSource = BaseInfoCtrl.GetDepartmentData(0);

            //初始化组团人列表
            shopLookUpEdit.DataSource = ShopCtrl.GetShopNameAndIDInfo();

            btnRefresh.PerformClick();
        }
            #endregion
        //查询团数据
        private void btnRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DateTime dateStart = DateTime.Parse(this.dateStart.EditValue.ToString());
            DateTime dateEnd = DateTime.Parse(this.dateEnd.EditValue.ToString());

            string shopId = tbShop.EditValue == null ? "" : tbShop.EditValue.ToString();
            string verifyState = tbVerifyState.EditValue.ToString();
            _dtCommissionData = CommissionCtrl.GetCommissionData(dateStart, dateEnd, shopId,verifyState);//根据选择的部门过滤
            commissionGridControl.DataSource = AddGridSelectedColumn(_dtCommissionData);
        }

        private void commissionGridView_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            if (e.Clicks == 2)
            {
                //状态为‘审核’时允许修改
                if (commissionGridView.GetRowCellValue(e.RowHandle, "VerifyState").ToString() == ShopCommissionEnum.审核.ToString())
                {
                    CommissionFinanceVerifyModifyForm mform = new CommissionFinanceVerifyModifyForm(_dtCommissionData, e.RowHandle);
                    mform.Owner = this;
                    mform.ShowDialog();
                }
                else
                    MessageBox.Show("此项数据的当前状态为‘" + commissionGridView.GetRowCellValue(e.RowHandle, "VerifyState").ToString() + "’不能修改！","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
        }


        #region 修改、提交按钮事件处理
    
        private void btnModify_Click(object sender, EventArgs e)
        {
            if (_dtCommissionData == null || _dtCommissionData.Rows.Count == 0) return;

            commissionGridView_RowClick(sender, new DevExpress.XtraGrid.Views.Grid.RowClickEventArgs(new DevExpress.Utils.DXMouseEventArgs(MouseButtons.Left, 2, 0, 0, 0), commissionGridView.FocusedRowHandle));
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            string consumptionId = "";
            int[] rowIds = MutilRow(commissionGridView);
            
            if (rowIds.Length <= 0) return;

            for (int i = 0; i < rowIds.Length; i++)
            {
                if (commissionGridView.GetRowCellValue(rowIds[i], "VerifyState").ToString() != ShopCommissionEnum.审核.ToString()) 
                {
                    MessageBox.Show("选中项中存在非‘审核’项，不能进行终结");
                    return;
                }
            }

            if (MessageBox.Show("确认终结选中的项？", "终结确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                for (int i = 0; i < rowIds.Length; i++)
                {
                    consumptionId = commissionGridView.GetRowCellValue(rowIds[i], "ConsumptionID").ToString();
                    if (CommissionCtrl.UpdateCommissionStateToFinal(consumptionId) >= 1)
                    {
                        commissionGridView.SetRowCellValue(rowIds[i], "VerifyState", ShopCommissionEnum.终结);//更新单元格信息，免除从数据库刷新
                    }
                    else
                    {
                        MessageBox.Show("系统故障，终结失败！！");
                    }
                }
                MessageBox.Show("已完成所有选中项的‘终结’工作");
            }
        }
        #endregion

        private void commissionGridView_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            if (e.RowHandle < 0) return;
            object selected = commissionGridView.GetRowCellValue(e.RowHandle, "Selected");

            if (selected == DBNull.Value) return;

            if (Convert.ToBoolean(selected) == true)
            {
                e.Appearance.BackColor = Color.Wheat;
                e.Appearance.BackColor2 = Color.Azure;
            }
            else
            {
                e.Appearance.BackColor = Color.Empty;
                e.Appearance.BackColor2 = Color.Empty;
            }
        }

        #region 打钩选择处理
        private void commissionGridView_CustomDrawColumnHeader(object sender, ColumnHeaderCustomDrawEventArgs e)
        {
            if (e.Column != null && e.Column.FieldName == "Selected")
            {
                e.Info.InnerElements.Clear();
                e.Painter.DrawObject(e.Info);
                DevGridCheckControl.DrawCheckBox(e, m_checkStatus);
                e.Handled = true;
            }
        }

        private void commissionGridView_Click(object sender, EventArgs e)
        {
            if (DevGridCheckControl.ClickGridCheckBox(this.commissionGridView, "Selected", m_checkStatus))
            {
                m_checkStatus = !m_checkStatus;
            }
        }

        private int[] MutilRow(GridView gv)
        {
            object selected;
            for (int i = 0; i < gv.RowCount; i++)
            {
                selected = gv.GetRowCellValue(i, "Selected");

                if (string.IsNullOrEmpty(selected.ToString())) continue;

                if (Convert.ToBoolean(selected) == true)
                {
                    gv.SelectRow(i);
                }
            }
            return gv.GetSelectedRows();
        }
        #endregion
    }
}